#include<func.h>
#define NUM 20000000
int numTest =0;
typedef struct ShareRes_s
{
    pthread_mutex_t mutex;
}ShareRes_t;
//线程
void* pthreadFunc (void* args)
{
    ShareRes_t* share = (ShareRes_t*)args;
    for(int i =0;i<NUM;i++)
    {
        //加锁操作
        pthread_mutex_lock(&share->mutex);
        numTest++;
        pthread_mutex_unlock(&share->mutex);
    }
    pthread_exit(NULL);
}

int main(void)
{
    ShareRes_t share;
    pthread_mutex_init(&share.mutex,NULL);//初始化锁
    pthread_t td1;//线程
    pthread_create(&td1,NULL,pthreadFunc,&share);

    struct timeval begTime,endTime;
    gettimeofday(&begTime,NULL);

    //for 加锁操作
    for(int i=0;i<NUM;i++)
    {
        pthread_mutex_lock(&share.mutex);
        numTest++;
        pthread_mutex_unlock(&share.mutex);
    }
    pthread_join(td1,NULL);
    gettimeofday(&endTime,NULL);
    printf("total time = %ld us\n",(endTime.tv_sec-begTime.tv_sec)*1000000+endTime.tv_usec);
    printf("this num = %d\n",numTest);
    return 0;
}